package com.dt.platform.hr.page;

import com.dt.platform.constants.enums.common.StatusEnableEnum;
import com.dt.platform.constants.enums.hr.LearnTaskStatusEnum;
import com.dt.platform.domain.hr.Learn;
import com.dt.platform.domain.hr.LearnRelease;
import com.dt.platform.domain.hr.LearnTask;
import com.dt.platform.domain.hr.LearnTaskVO;
import com.dt.platform.proxy.hr.LearnReleaseServiceProxy;
import com.dt.platform.proxy.hr.LearnTaskServiceProxy;
import com.github.foxnic.api.transter.Result;
import org.github.foxnic.web.framework.view.controller.ViewController;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import com.dt.platform.proxy.hr.LearnServiceProxy;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 * 学习课程模版页面控制器
 * </p>
 * @author 金杰 , maillank@qq.com
 * @since 2024-09-14 11:31:30
*/

@Controller("HrLearnPageController")
@RequestMapping(LearnPageController.prefix)
public class LearnPageController extends ViewController {

	public static final String prefix="business/hr/learn";

	private LearnServiceProxy proxy;

	/**
	 * 获得代理对象<br>
	 * 1、单体应用时，在应用内部调用；<br>
	 * 2、前后端分离时，通过配置，以Rest方式调用后端；<br>
	 * 3、微服务时，通过feign调用; <br>
	 * */
	public LearnServiceProxy proxy() {
		if(proxy==null) {
			proxy=LearnServiceProxy.api();
		}
		return proxy;
	}

	/**
	 * 学习课程 功能主页面
	 */
	@RequestMapping("/learn_view.html")
	public String learnView(Model model,HttpServletRequest request,String role,String learnId,String learnReleaseId,String taskId) {

		boolean result=true;
		model.addAttribute("learnId",learnId);
		model.addAttribute("learnReleaseId",learnReleaseId);
		model.addAttribute("taskId",taskId);
		// review 查看
		// task
		model.addAttribute("role",role);
		if("review".equals(role)){
			Result<Learn> learnRes=LearnServiceProxy.api().getById(learnId);
			if(learnRes.isSuccess()){
				Learn learn=learnRes.getData();
				model.addAttribute("learnData",learn);
			}
		}else if("task".equals(role)){
			Result<LearnTask> learnTaskRes=LearnTaskServiceProxy.api().getById(taskId);
			if(learnTaskRes.isSuccess()){
				LearnTask task=learnTaskRes.getData();
				model.addAttribute("taskData",task);
				model.addAttribute("learnData",task.getLearn());
				//判断当前是可以进入学习
				if(StatusEnableEnum.DISABLE.code().equals(task.getLearnRelease().getStatus())){
					model.addAttribute("msg","当前学习任务未启用，请联系管理员进行操作!");
					result=false;
					model.addAttribute("result",result);
					return getTemplatePath(prefix,"learn_view");
				}
				//是否已经作废
				if(task.getStatus().equals(LearnTaskStatusEnum.CANCEL.code())){
					model.addAttribute("msg","当前学习任务已作废");
					result=false;
					model.addAttribute("result",result);
					return getTemplatePath(prefix,"learn_view");
				}
				//判断当前时间是否到
				Date curTime=new Date();
				if(curTime.before(task.getLearnRelease().getSdate())){
					String format="yyyy-MM-dd HH:mm:ss";
					SimpleDateFormat sdf=new SimpleDateFormat(format);
					model.addAttribute("msg","当前学习任务时间未到，开始时间为:"+sdf.format(task.getLearnRelease().getSdate()));
					result=false;
					model.addAttribute("result",result);
					return getTemplatePath(prefix,"learn_view");
				}
				//第一次打开学习任务，插入开始时间
				if(task.getStartTime()==null){
					//插入一条数据
					LearnTaskVO taskUpdate=LearnTaskVO.create();
					taskUpdate.setStartTime(new Date());
					taskUpdate.setId(taskId);
					LearnTaskServiceProxy.api().update(taskUpdate);
				}
			}
		}else{
			//根据learnRelaseId来完成学习任务未实现
		}
		model.addAttribute("result",result);
		return getTemplatePath(prefix,"learn_view");
	}


	/**
	 * 学习课程 功能主页面
	 */
	@RequestMapping("/learn_list.html")
	public String list(Model model,HttpServletRequest request) {
		return getTemplatePath(prefix,"learn_list");
	}

	/**
	 * 学习课程 表单页面
	 */
	@RequestMapping("/learn_form.html")
	public String form(Model model,HttpServletRequest request , String id) {
		return getTemplatePath(prefix,"learn_form");
	}
}